package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.common.StringUtil;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.dao.ErTongBJKDao;
import com.jy.rhin.infra.cda.dao.ErTongJKTJDao;
import com.jy.rhin.infra.cda.ertong.ErTongJKTJBaseVO;
import com.jy.rhin.infra.cda.ertong.LiangSuiRTJKTJVO;
import com.jy.rhin.infra.cda.ertong.SanSuiRTJKTJVO;
import com.jy.rhin.infra.cda.ertong.YISUIETJKTJVO;
import com.jy.rhin.infra.cda.xinshenger.ErTongBJKVo;
import com.jy.rhin.infra.cda.zhiyu.XingBieDM;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.body.ertongjktj.*;
import com.jy.rhin.model.header.Author;
import com.jy.rhin.model.header.Custodian;
import com.jy.rhin.model.header.Patient;
import com.jy.rhin.model.header.PatientRole;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.Objects;

//儿童健康体检
@Service
public class ErTongJKTJApplication {

    private static final String FTL_PATH = "/04ertongjktj.ftl";
    @Resource
    private CDACreator cdaCreator;
    @Resource
    private ErTongJKTJDao erTongJKTJDao;
    @Resource
    private ErTongBJKDao erTongBJKDao;

    @Resource
    private HospitalAPI hospitalAPI;

    public String createErTongJKTJ(String jiLuBiaoHao, String yiliaojgdm, String tablename) {
        ErTongJKTJ doc = null;
        switch (tablename) {
            case "FY_YISUIETJKJC":
                doc = create1Sui(jiLuBiaoHao, yiliaojgdm);
                break;
            case "FY_LIANGSUIETJKJC":
                doc = create2Sui(jiLuBiaoHao, yiliaojgdm);
                break;
            case "FY_SANSUIETJKJC":
                doc = create3Sui(jiLuBiaoHao, yiliaojgdm);
                break;
        }
        return cdaCreator.create(doc, FTL_PATH);
    }


    private HospitalDTO getHospital(String fuwujgdm) {
        HospitalDTO hospital = hospitalAPI.info(fuwujgdm).getData();
        if (Objects.isNull(hospital)) {
            throw new CDAException("机构数据不存在", null);
        }
        return hospital;
    }

    private ErTongBJKVo getErTongBJK(String fuwujgdm, String gerendabs) {
        ErTongBJKVo erTongBJKVo = erTongBJKDao.getByGERENDABS(fuwujgdm, gerendabs);
        if (Objects.isNull(erTongBJKVo)) {
            throw new CDAException("儿童保健卡数据不存在", null);
        }
        return erTongBJKVo;
    }

    //1岁儿童
    private ErTongJKTJ create1Sui(String jiLuBiaoHao, String yiliaojgdm) {
        ErTongJKTJ doc = new ErTongJKTJ();
        YISUIETJKTJVO vo = erTongJKTJDao.get1ByBH(jiLuBiaoHao, yiliaojgdm);
        if (Objects.isNull(vo)) {
            throw new CDAException("1-8月龄儿童健康检查数据不存在", null);
        }
        createErTongJKTJ(doc, vo, yiliaojgdm);
        return doc;
    }

    //2岁儿童
    private ErTongJKTJ create2Sui(String jiLuBiaoHao, String yiliaojgdm) {
        ErTongJKTJ doc = new ErTongJKTJ();
        LiangSuiRTJKTJVO vo = erTongJKTJDao.get2ByBH(jiLuBiaoHao, yiliaojgdm);
        if (Objects.isNull(vo)) {
            throw new CDAException("12-30月龄儿童健康检查数据不存在", null);
        }
        createErTongJKTJ(doc, vo, yiliaojgdm);
        return doc;
    }

    //3岁儿童
    private ErTongJKTJ create3Sui(String jiLuBiaoHao, String yiliaojgdm) {
        ErTongJKTJ doc = new ErTongJKTJ();
        SanSuiRTJKTJVO vo = erTongJKTJDao.get3ByBH(jiLuBiaoHao, yiliaojgdm);
        if (Objects.isNull(vo)) {
            throw new CDAException("3-6岁儿童健康检查数据不存在", null);
        }
        createErTongJKTJ(doc, vo, yiliaojgdm);
        return doc;
    }

    private void createErTongJKTJ(ErTongJKTJ doc, ErTongJKTJBaseVO vo, String yiliaojgdm) {
        HospitalDTO hospital = getHospital(vo.getFUWUJGBH());
        ErTongBJKVo erTongBJK = getErTongBJK(yiliaojgdm, vo.getGERENDABS());

        vo.setErTongBJK(erTongBJK);
        vo.setZhengJianHM(vo.getZhengJianHM());

        tranData(vo, hospital);
        doc.setEffectiveTime(CDAUtil.toD15(vo.getJILURQ()));
        doc.setId(String.valueOf(vo.getId()));
        doc.setPatientRole(createPatientRole(vo));
        doc.setAuthor(createAuthor(vo, hospital));
        doc.setCustodian(createCustodian(hospital));

        doc.setShengMingTZ(createShengMingTZ(vo));
        doc.setYiBanWaiGuan(createYiBanWG(vo));
        doc.setPiFuJianCha(createPiFuJianCha(vo));
        doc.setXinMenBHQK(createXinMenBHQK(vo));
        doc.setTouWei(createTouWei(vo));
        doc.setJinBu(createJinBu(vo));
        doc.setYanBu(createYanBu(vo));
        doc.setErBu(createErBu(vo));
        doc.setTingLiSC(createTingLiSC(vo));
        doc.setKouQiangJC(createKouQiangJC(vo));
        doc.setXinZang(createXinZang(vo));
        doc.setHuXiXT(createHuXiXT(vo));
        doc.setFuBuJC(createFuBuJC(vo));
        doc.setQiDaiTL(createQiDaiTL(vo));
        doc.setSiZhi(createSiZhi(vo));
        doc.setJiZhu(createJiZhu(vo));
        doc.setShengZhiQi(createShengZhiQi(vo));
        doc.setZhiChang(createZhiChang(vo));
        doc.setHuWaiYD(createHuWaiYD(vo));
        doc.setLiangCiSFJHBQK(createLiangCiSFJHBQK(vo));
        doc.setShiYanSJC(createShiYanSJC(vo));
        doc.setYongYao(createYongYao(vo));
        doc.setJianKangPG(createJianKangPG(vo));
        doc.setJianKangZD(createJianKangZD(vo));
        doc.setZhuanZhenJY(createZhuanZhenJY(vo));
        doc.setXiaCiSFAP(createXiaCiSFAP(vo));
    }

    private void tranData(ErTongJKTJBaseVO vo, HospitalDTO hospital) {
        vo.setGERENDABS17(CDAUtil.formatNumber(vo.getGERENDABS()));
        vo.setFUWUJGBH(hospital.getOutId());
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(hospital.getOutId());
        custodian.setName(hospital.getName());
        custodian.setTelecom(hospital.getDanWeiDH());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    Author createAuthor(ErTongJKTJBaseVO info, HospitalDTO hospital) {
        Author author = new Author();
        //todo 需要医生工号
        author.setId(info.getGERENDABS());
        author.setTime(toD8(info.getSUIFANGRQ()));
        author.setName(info.getSUIFANGYSXM());
        author.setOrganizationId(hospital.getOutId());
        author.setOrganizationName(hospital.getName());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    PatientRole createPatientRole(ErTongJKTJBaseVO info) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(info.getGERENDABS17());
        patientRole.setPatient(createPatient(info));
        return patientRole;
    }

    Patient createPatient(ErTongJKTJBaseVO info) {
        Patient patient = new Patient();
        patient.setName(info.getXINGMING());
        patient.setId(StringUtil.isNullOrEmpty(info.getZhengJianHM()) ? info.getGERENDABS() : info.getZhengJianHM());
        if (info.getErTongBJK() != null) {
            if (info.getErTongBJK().getERTONGXB() != null) {
                String xingbiemc = XingBieDM.getDescByCode(info.getErTongBJK().getERTONGXB());
                patient.setAdministrativeGender(new CodeObject(info.getErTongBJK().getERTONGXB().toString(), xingbiemc));
            }
            if (info.getErTongBJK().getCHUSHENGRQ() != null)
                patient.setBirthTime(toD8(info.getErTongBJK().getCHUSHENGRQ()));
        }
        return patient;
    }

    private ShengMingTZ createShengMingTZ(ErTongJKTJBaseVO vo) {
        ShengMingTZ model = new ShengMingTZ();
        model.setShenChang(vo.getSHENCHANG() == null ? "0.0" : vo.getSHENCHANG().setScale(1, RoundingMode.FLOOR).toPlainString());
        model.setTiZhong(vo.getTIZHONG() == null ? "0.0" : vo.getTIZHONG().setScale(1, RoundingMode.FLOOR).toPlainString());
        return model;
    }

    private YiBanWaiGuan createYiBanWG(ErTongJKTJBaseVO vo) {
        YiBanWaiGuan model = new YiBanWaiGuan();
        if (StringUtil.isNullOrEmpty(vo.getMIANSEDM())) {
            model.setXinShengErMS(new CodeObject("9", "其他"));
        } else {
            model.setXinShengErMS(new CodeObject(vo.getMIANSEDM(), vo.getMIANSEMC()));
        }

        if (StringUtil.isNullOrEmpty(vo.getKEYIGLBZZDM())) {
            model.setKeYiGLBZZ(new CodeObject("1", "无"));
        } else {
            model.setKeYiGLBZZ(new CodeObject(vo.getKEYIGLBZZDM(), vo.getKEYIGLBZZMC()));
        }

        if (StringUtil.isNullOrEmpty(vo.getKEYIGLBTZDM())) {
            model.setKeYiGLBTZ(new CodeObject("01", "无"));
        } else {
            model.setKeYiGLBTZ(new CodeObject(vo.getKEYIGLBTZDM(), vo.getKEYIGLBTZMC()));
        }
        model.setBuTaiYCBZ("false");
        return model;
    }

    private PiFuJianCha createPiFuJianCha(ErTongJKTJBaseVO vo) {
        PiFuJianCha model = new PiFuJianCha();
        model.setPiFuYCBZ("2".equals(vo.getPIFUYCBZ()) ? "true" : "false");
        return model;
    }

    private XinMenBHQK createXinMenBHQK(ErTongJKTJBaseVO vo) {
        XinMenBHQK model = new XinMenBHQK();
        model.setQianXinBHBZ("1".equals(vo.getQIANXINBHBZ()) ? "true" : "false");
        model.setQianXinHJ(vo.getQIANXINHJ() == null ? "0.0" : vo.getQIANXINHJ().setScale(1, RoundingMode.FLOOR).toPlainString());
        model.setQianXinZJ(vo.getQIANXINZJ() == null ? "0.0" : vo.getQIANXINZJ().setScale(1, RoundingMode.FLOOR).toPlainString());

        model.setQianXinZL(new CodeObject("1", "正常"));
        return model;
    }

    private TouWei createTouWei(ErTongJKTJBaseVO vo) {
        TouWei model = new TouWei();
        model.setTouWei(vo.getTOUWEI() == null ? "0.0" : vo.getTOUWEI().setScale(1, RoundingMode.FLOOR).toPlainString());
        return model;
    }

    private JinBu createJinBu(ErTongJKTJBaseVO vo) {
        JinBu model = new JinBu();
        model.setJinBuBKBZ("2".equals(vo.getJINBUBKNZ()) ? "true" : "false");
        model.setJinBuBKJCJGMS(StringUtil.isNullOrEmpty(vo.getJINBUBKXS()) ? "未见异常" : vo.getJINBUBKXS());
        return model;
    }

    private YanBu createYanBu(ErTongJKTJBaseVO vo) {
        YanBu model = new YanBu();

        model.setZuoYanLYYSLZ(vo.getZUOYANLYYSLZ() == null ? "0.0" : vo.getZUOYANLYYSLZ().setScale(1, RoundingMode.FLOOR).toPlainString());
        model.setZuoYanJZYSLZ(vo.getZUOYANJZYSLZ() == null ? "0.0" : vo.getZUOYANJZYSLZ().setScale(1, RoundingMode.FLOOR).toPlainString());
        model.setYouYanLYYSLZ(vo.getYOUYANLYYSLZ() == null ? "0.0" : vo.getYOUYANLYYSLZ().setScale(1, RoundingMode.FLOOR).toPlainString());
        model.setYouYanJZYSLZ(vo.getYOUYANJZYSLZ() == null ? "0.0" : vo.getYOUYANJZYSLZ().setScale(1, RoundingMode.FLOOR).toPlainString());
        model.setYanWaiGJCYCBZ("2".equals(vo.getYANWAIGYCBZ()) ? "true" : "false");
        model.setYanWaiGJCYCJGMS(StringUtil.isNullOrEmpty(vo.getYANWAIGYCXS()) ? "未见异常" : vo.getYANWAIGYCXS());
        return model;
    }

    private ErBu createErBu(ErTongJKTJBaseVO vo) {
        ErBu model = new ErBu();
        model.setYiCBZ("2".equals(vo.getERWAIGYCBZ()) || StringUtil.isNullOrEmpty(vo.getERWAIGYCBZ()) ? "true" : "false");
        model.setErWaiGJCYCJGMS(StringUtil.isNullOrEmpty(vo.getERWAIGYCXS()) ? "未见异常" : vo.getERWAIGYCXS());
        return model;
    }

    private TingLiSC createTingLiSC(ErTongJKTJBaseVO vo) {
        TingLiSC model = new TingLiSC();
        if (StringUtil.isNullOrEmpty(vo.getXINSHENGETLSCJG())) {
            model.setTingLiSC(new CodeObject("1", "通过"));
        } else {
            model.setTingLiSC(new CodeObject(vo.getXINSHENGETLSCJG(), "1".equals(vo.getXINSHENGETLSCJG())? "通过" :"未通过"));
        }
        return model;
    }

    private KouQiangJC createKouQiangJC(ErTongJKTJBaseVO vo) {
        KouQiangJC model = new KouQiangJC();
        model.setKouQianYCBZ("2".equals(vo.getKOUQIANGYCBZ()) ? "true" : "false");
        model.setKouQianJCYCJGMS(StringUtil.isNullOrEmpty(vo.getKOUQIANGYCXS()) ? "无" : vo.getKOUQIANGYCXS());
        model.setChuYaShu(StringUtil.isNullOrEmpty(vo.getCHUYAS()) ? "0" : vo.getCHUYAS());
        model.setQuChiShu(StringUtil.isNullOrEmpty(vo.getQUCHIS()) ? "0" : vo.getQUCHIS());
        return model;
    }

    private XinZang createXinZang(ErTongJKTJBaseVO vo) {
        XinZang model = new XinZang();
        model.setXinZangTZYCBZ("false");
        model.setXinZangTZYCMS("未见异常");
        return model;
    }

    private HuXiXT createHuXiXT(ErTongJKTJBaseVO vo) {
        HuXiXT model = new HuXiXT();
        model.setFeiBuTZYCBZ("false");
        model.setFeiBuTZYCMS("未见异常");
        return model;
    }

    private FuBuJC createFuBuJC(ErTongJKTJBaseVO vo) {
        FuBuJC model = new FuBuJC();
        model.setFuBuJCYCBZ("2".equals(vo.getFUBUCZYCBZ()) ? "true" : "false");
        model.setFuBuJCYCMS(StringUtil.isNullOrEmpty(vo.getFUBUCZYCXS()) ? "无" : vo.getFUBUCZYCXS());
        return model;
    }

    private QiDaiTL createQiDaiTL(ErTongJKTJBaseVO vo) {
        QiDaiTL model = new QiDaiTL();
        model.setQiDaiTLBZ("2".equals(vo.getFUBUCZYCBZ()) ? "true" : "false");
        if (StringUtil.isNullOrEmpty(vo.getQIDAIJCJGDM())) {
            model.setQiDaiTLJG(new CodeObject("9", "其他"));
        } else {
            model.setQiDaiTLJG(new CodeObject(vo.getQIDAIJCJGDM(), vo.getQIDAIJCJGMC()));
        }
        return model;
    }

    private SiZhi createSiZhi(ErTongJKTJBaseVO vo) {
        SiZhi model = new SiZhi();
        model.setSiZhiHDYCBZ("2".equals(vo.getSIZHIYCBZ()) ? "true" : "false");
        model.setSiZhiHDYCMS(StringUtil.isNullOrEmpty(vo.getSIZHIYCXS()) ? "未见异常" : vo.getSIZHIYCXS());
        return model;
    }

    private JiZhu createJiZhu(ErTongJKTJBaseVO vo) {
        JiZhu model = new JiZhu();
        model.setJiZhuYCBZ("false");
        model.setJiZhuYCMS("无");
        return model;
    }

    private ShengZhiQi createShengZhiQi(ErTongJKTJBaseVO vo) {
        ShengZhiQi model = new ShengZhiQi();
        model.setShengZhiQiJCYCBZ("2".equals(vo.getWAISHENGZQYCBZ()) ? "true" : "false");
        model.setShengZhiQiJCYCMS(StringUtil.isNullOrEmpty(vo.getWAISHENGZQYCXS()) ? "未见异常" : vo.getWAISHENGZQYCXS());
        return model;
    }

    private ZhiChang createZhiChang(ErTongJKTJBaseVO vo) {
        ZhiChang model = new ZhiChang();
        model.setGanMenJCYCBZ("2".equals(vo.getGANGMENYCBZ()) ? "true" : "false");
        model.setGanMenJCYCMS(StringUtil.isNullOrEmpty(vo.getGANGMENYCXS()) ? "未见异常" : vo.getGANGMENYCXS());
        return model;
    }

    private HuWaiYD createHuWaiYD(ErTongJKTJBaseVO vo) {
        HuWaiYD model = new HuWaiYD();
        model.setHUWAIHDSC(vo.getHUWAIHDSC() == null ? "0.0" : vo.getHUWAIHDSC().setScale(1, RoundingMode.FLOOR).toPlainString());
        return model;
    }

    private LiangCiSFJHBQK createLiangCiSFJHBQK(ErTongJKTJBaseVO vo) {
        LiangCiSFJHBQK model = new LiangCiSFJHBQK();
        model.setLiangCiSFHBBZ("1".equals(vo.getLIANGCISFJHBDM()) ? "true" : "false");
        model.setLIANGCISFJHFYZYCS(StringUtil.isNullOrEmpty(vo.getLIANGCISFJHFYZYCS()) ? "0" : vo.getLIANGCISFJHFYZYCS());
        model.setLIANGCISFJHFXZYCS(StringUtil.isNullOrEmpty(vo.getLIANGCISFJHFXZYCS()) ? "0" : vo.getLIANGCISFJHFXZYCS());
        model.setLIANGCISFJYWSZYCS(StringUtil.isNullOrEmpty(vo.getLIANGCISFJYWSZYCS()) ? "0" : vo.getLIANGCISFJYWSZYCS());
        model.setLIANGCISFJHQTBXS(StringUtil.isNullOrEmpty(vo.getLIANGCISFJHQTBXS()) ? "0" : vo.getLIANGCISFJHQTBXS());
        return model;
    }

    private ShiYanSJC createShiYanSJC(ErTongJKTJBaseVO vo) {
        ShiYanSJC model = new ShiYanSJC();
        model.setXueHongDBZB(StringUtil.isNullOrEmpty(vo.getXUEHONGDB()) || "-99.0".equals(vo.getXUEHONGDB()) ? "0" : new BigDecimal(vo.getXUEHONGDB()).setScale(0,BigDecimal.ROUND_HALF_UP).toString());
        return model;
    }

    private YongYao createYongYao(ErTongJKTJBaseVO vo) {
        YongYao model = new YongYao();
        model.setDanCiYYJL("0");
        model.setYaoWuSYPC("0");
        model.setYaoPinMC("无");
        return model;
    }

    private JianKangPG createJianKangPG(ErTongJKTJBaseVO vo) {
        JianKangPG model = new JianKangPG();


        model.setNianLingBSG(getSZFY(vo.getNIANLINGBSGPJDM()));
        model.setNianLingBTZ(getSZFY(vo.getNIANLINGBTZPJDM()));

        model.setFaYuPG(new CodeObject(vo.getYINGYANGPJDM(), vo.getYINGYANGPJMC()));
        model.setFaYuPGTGBZ("1".equals(vo.getERTONGFYPGTGBZ()) ? "true" : "false");

        return model;
    }

    private CodeObject getSZFY(String code){
        CodeObject c = new CodeObject();
        if("1".equals(code)){
            c = new CodeObject("1","上");
        } else if ("2".equals(code)) {
            c = new CodeObject("21","中上");
        } else if ("3".equals(code)) {
            c = new CodeObject("2","中");
        } else if ("4".equals(code)) {
            c = new CodeObject("24","中下");
        } else if ("5".equals(code)) {
            c = new CodeObject("3","下");
        }else{
            c = new CodeObject("2","中");
        }
        return c;
    }

    private JianKangZD createJianKangZD(ErTongJKTJBaseVO vo) {
        JianKangZD model = new JianKangZD();
        model.setErTongJKZD(new CodeObject("9", "其他"));
        return model;
    }

    private ZhuanZhenJY createZhuanZhenJY(ErTongJKTJBaseVO vo) {
        ZhuanZhenJY model = new ZhuanZhenJY();
        model.setZhuanZhenBZ(!(StringUtil.isNullOrEmpty(vo.getZHUANZHENBZ()) || "9".equals(vo.getZHUANZHENBZ())) ? "true" : "false");
        model.setZhuanZhenYY(StringUtil.isNullOrEmpty(vo.getZHUANZHENYY()) ? "无" : vo.getZHUANZHENYY());
        model.setZhuanRuKSMC("无");
        model.setZhuanRuYLJGMC(StringUtil.isNullOrEmpty(vo.getZHUANRUYLJGMC()) ? "无" : vo.getZHUANRUYLJGMC());
        return model;
    }

    private XiaCiSFAP createXiaCiSFAP(ErTongJKTJBaseVO vo) {
        XiaCiSFAP model = new XiaCiSFAP();
        model.setXiaCISFRQ(toD8(vo.getXIACISFRQ()));
        return model;
    }


    private String toD8(LocalDateTime date) {
        if (date == null) return null;
        return Converter.toString(date, "yyyyMMdd");
    }
}
